package com.controller;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import net.sf.json.JSONObject;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.entity.Owner;
import com.service.OwnerService;
@Controller
@RequestMapping("/owner")
public class OwnerController {
	@Resource
	OwnerService ownerService;
	/**
	 * 需求分析
	 * 根据业主输入的手机号码判断数据库中是否存在
	 * 存在     -- 将业主和注册过的用户信息读取出来
	 * 不存在 -- 为业主创建一个手机号码保存数据库
	 */
	@RequestMapping(value="/selectPhone",produces="text/html;charset=UTF-8")
	@ResponseBody
	public String selectPhone(HttpServletRequest request,ModelMap map,Owner addowner){
		/*String yphone=request.getParameter("yphone");			
		Map<String,Object> parms=new HashMap<String, Object>();
		parms.put("yphone", yphone);				
		Owner owners=ownerService.selectPhone(parms);			
		if(owners!=null){										
			Owner owner=ownerService.selectUploadfiles(owners.getYnumber());
			map.addAttribute("owner", owner);
			JSONObject json=JSONObject.fromObject(map);
			return json.toString();	
		}else{
			ownerService.insert(addowner);											
			Map<String,Object> mapPhone=new HashMap<String, Object>();
			mapPhone.put("yphone", addowner.getYphone());					
			Owner mapPhones=ownerService.selectPhone(mapPhone);					
			JSONObject json=JSONObject.fromObject(mapPhones);
			return json.toString();	
		}*/
		return null;
	}
	
	@RequestMapping(value="/newselectPhone",method=RequestMethod.POST,produces = "application/json; charset=utf-8")
	@ResponseBody
	public String newselectPhone(String yphone,ModelMap map){
		/*String yphone=request.getParameter("yphone");		
		Map<String,Object> parms=new HashMap<String, Object>();
		parms.put("yphone", yphone);					
		Owner owners=ownerService.selectPhone(parms);		
		if(owners!=null){									
			Owner newowner=ownerService.selectUploadfiles(owners.getYnumber());
			map.addAttribute("newowner", newowner);
			new JSONObject();
			JSONObject jsonObject=JSONObject.fromObject(map);
			return jsonObject.toString();
		}else{
			parms.put("result", "查询失败");					
			new JSONObject();
			JSONObject jsonObject=JSONObject.fromObject(parms);
			return jsonObject.toString();
		}*/					
			//int i=Integer.valueOf(yphone).intValue();
			Owner newowner=ownerService.selectUploadfiles(yphone);
			map.addAttribute("newowner", newowner);
			new JSONObject();
			JSONObject jsonObject=JSONObject.fromObject(map);
			return jsonObject.toString();
	



		
	}
	/**
	 * 添加一个手机号码
	 * @param addowner
	 * @return
	 */
	@RequestMapping(value="/addPhone",produces="text/html;charset=UTF-8")
	@ResponseBody
	public String addPhone(Owner addowner){
		ownerService.insert(addowner);											
		Map<String,Object> mapPhone=new HashMap<String, Object>();
		mapPhone.put("yphone", addowner.getYphone());					
		Owner mapPhones=ownerService.selectPhone(mapPhone);
		JSONObject json=JSONObject.fromObject(mapPhones);
		return json.toString();								
	}
	/**
	 * 查询手机号码
	 * @param request
	 * @param map
	 * @return
	 */
	@RequestMapping(value="/selectOnePhone",produces="text/html;charset=UTF-8")
	@ResponseBody
	public String selectOnePhone(HttpServletRequest request,ModelMap map){
		String yphone=request.getParameter("yphone");	   	//小程序输入的手机号码	
		Map<String,Object> parms=new HashMap<String, Object>();
		parms.put("yphone", yphone);
		Owner owners=ownerService.selectPhone(parms);	   	//查询数据库
		new JSONObject();
		JSONObject json=JSONObject.fromObject(owners);
		return json.toString();
	}
	/**
	 * 根据id查询
	 * @param request
	 * @return
	 */
	@RequestMapping("/selectByid")
	@ResponseBody
	public String selectByid(HttpServletRequest request){
		
		String ynumber=request.getParameter("ynumber");		//小程序输入的手机号码	
		System.out.println(ynumber);
		Owner owners=ownerService.selectByPrimaryKey(ynumber);//查询数据库
		new JSONObject();
		JSONObject json=JSONObject.fromObject(owners);
		return json.toString();
		
	}
	/**
	 * 查询微信openid
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/selectOpenid",produces="text/html;charset=UTF-8")
	@ResponseBody
	public String selectOpenid(HttpServletRequest request){
		String openid=request.getParameter("openid");		//微信openid
		Map<String,Object> parms=new HashMap<String, Object>();
		parms.put("openid", openid);
		Owner owners = ownerService.selectOpenid(parms);	//查询数据库
		new JSONObject();
		JSONObject json=JSONObject.fromObject(owners);
		return json.toString();	
	}
}
